﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM; 


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryPromotionConfig : System.Web.UI.UserControl
    {

        SystemYearTypeBLL yearTypeBLL = new SystemYearTypeBLL();
     
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }



        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }




        public void bindYearCombo()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cbYear.DataSource = dt;
            cbYear.DataBind();
            cbYear.SelectedValue = curYear.ToString();
            cbYear_SelectedIndexChanged(null, null);
        }


        public string formatDateThai(string value)
        {

            //08/04/2553

            int pos = value.IndexOf("00:00:00"); 
            if(pos > 1)
            {
                value = value.Replace("00:00:00",""); 

            }


            string tmp = null;
            string[] tmp2 = null;
            tmp2 = value.Split('/');
            tmp = tmp2[1] + "/" + tmp2[0] + "/" + Convert.ToString(int.Parse(tmp2[2]) + 543);

            return tmp;
        }


        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
         



            if (DataItem != null)
            {
                //   cbGender.ListGenderComboBox();

                bindEmpType();
                bindYearCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {

                    SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();
                    string strDate;

                    lblPrmSeq.Text = DataBinder.Eval(DataItem, "prm_seq").ToString();
                    //strDate = DataBinder.Eval(DataItem, "prm_date").ToString();
                    //strDate = formatDateThai(strDate);
                    txtPrmDate.SetValue(DataBinder.Eval(DataItem, "prm_date", "{0:dd/MM/yyyy}").ToString());  

                    txtEmpQuota.Text = DataBinder.Eval(DataItem, "prm_quota").ToString();
                    txtPeriod.Text = DataBinder.Eval(DataItem, "prm_period").ToString();
                    
                    txtPromotionName.Text = DataBinder.Eval(DataItem, "prm_name").ToString();
                    cbYear.SelectedValue = DataBinder.Eval(DataItem, "prm_year").ToString();
                    cbEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();     




                    // lblSeq.Text = DataBinder.Eval(DataItem, "req_seq").ToString();
                    //txtNo.Text = DataBinder.Eval(DataItem, "seq").ToString();
                    //txtReason.Text = DataBinder.Eval(DataItem, "reason").ToString();
                    //cmbJobsite.SelectedValue = DataBinder.Eval(DataItem, "job_site_seq").ToString();
                    //cmbInqSubject.SelectedValue = DataBinder.Eval(DataItem, "inquire_seq").ToString();
                    //this.ViewState.Add("IsUserControlPostBack", true);
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }

        }



        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();
           

            string emp_type_seq;
            string prm_date;
            string prm_name = txtPromotionName.Text;
            decimal prm_quota;
            int prm_year,prm_period;
            prm_quota = Convert.ToDecimal(txtEmpQuota.Text) ;


            prm_year = int.Parse(cbYear.SelectedValue);
            prm_period = int.Parse(txtPeriod.Text);
            prm_date = txtPrmDate.GetStringValue();
            emp_type_seq = this.cbEmployeeType.SelectedValue;  

        obj.Insert_Promotions(prm_year,int.Parse(emp_type_seq)  ,prm_date,prm_period , prm_name, prm_quota, 1);     

    
 
        }


        private void bindEmpType()
        {
            PromotionDeptBLL obj = new PromotionDeptBLL();
            cbEmployeeType.DataSource = obj.GetEmployeeTypes();
            cbEmployeeType.DataTextField = "EMP_TYPE_NAME";
            cbEmployeeType.DataValueField = "EMP_TYPE_SEQ";
            cbEmployeeType.DataBind();
            cbEmployeeType.SelectedIndex = 0;
            cbEmployeeType_SelectedIndexChanged(null, null);
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {


            SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();


            string emp_type_seq;
            string prm_date;
            string prm_name = txtPromotionName.Text;
            decimal prm_quota;
            int prm_year, prm_period;
            prm_quota = Convert.ToDecimal(txtEmpQuota.Text);


            prm_year = int.Parse(cbYear.SelectedValue);
            prm_period = int.Parse(txtPeriod.Text);
            prm_date = txtPrmDate.GetStringValue();
            emp_type_seq = this.cbEmployeeType.SelectedValue;

            obj.UpdatePromotionsConfig (int.Parse(lblPrmSeq.Text ),  prm_year, int.Parse(emp_type_seq), prm_date, prm_period, prm_name, prm_quota, 1);    

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

            try
            {
                DataSet ds = yearTypeBLL.GetYearType(int.Parse(cbEmployeeType.SelectedValue));
                lblTypeYear.Text = "ปี :" + ds.Tables[0].Rows[0]["year_type_name"].ToString();
                int myYearNow = DateTime.Today.Year;

            }
            catch (Exception ex)
            {
                lblTypeYear.Text = "";
            }

        }

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {


        }
    }
}